<?php
/**
 * class Paging
 *
 * Description for class Paging
 *
 * @author: hqlam
*/
class Paging {
	private $pageIndex;
	private $rowPerPage;
	
	private $pagedList;
	private $pageCount;
	private $totalCount;
	private $displayCount;
	
	/**
	 * Constructor
	 *
	 */
	public function __construct($list, $pageIndex = 1, $rowPerPage = 5) {
		$this->pageIndex = $pageIndex;
		$this->rowPerPage = $rowPerPage;
		$this->totalCount = count($list);
		$this->pageCount = ceil($this->totalCount / $rowPerPage);
		$offset = ($pageIndex - 1) * $rowPerPage;
		$this->pagedList = array_slice($list, $offset, $rowPerPage);
		$this->displayCount = count($this->pagedList);
	}

	/**
	 * Get paged list
	 *
	 * @return array
	 */
	public function GetPagedList() {
		return $this->pagedList;
	}

	/**
	 * Get page count
	 *
	 * @return array
	 */
	public function GetPageCount() {
		return $this->pageCount;
	}
	
	/**
	 * Get paging navigation
	 *
	 * @return html string
	 */
	public function GetPagingNav($cssClass = "pagingNav") {
		$pageIndex = $this->pageIndex;
		$pageCount = $this->pageCount;
		
		$queryStr = '';
		foreach ($_GET as $key => $value) {
			if ($key != 'page') $queryStr .= "$key=$value&";			
		}
		$li = '<li><a href="?' . $queryStr . 'page=%d">%s</a></li>';
		
		$html = '<ul class="' . $cssClass . '">';
		// previous
		if ($pageIndex > 1) { 
			$html .= sprintf($li, $pageIndex - 1, '< Trước');
			for($i = $pageIndex-1, $k = 1; $i > 1 && $k < 3; $i--, $k++);
			for($j = $i; $j < $pageIndex; $j++) $html .= sprintf($li, $j, $j);
		}
		// current
		$html .= "<li class=\"current\">$pageIndex</li>";
		// next
		if ($pageIndex < $pageCount) {
			for($i = $pageIndex+1, $k = 1; $i <= $pageCount && $k < 5; $i++, $k++) $html .= sprintf($li, $i, $i);
			$html .= sprintf($li, $pageIndex + 1, 'Kế >');
		}
		
		$html .= '</ul>';
		return $html;	
	}

}
?>